package com.simulation.service.impl;

import cn.hutool.core.util.StrUtil;
import com.simulation.service.ExecutorService;
import com.simulation.utils.Consts;
import com.simulation.utils.LinuxShellComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author zhangxn
 * @date 2022/12/4  11:32
 */
@Service
public class ExecutorServiceImpl implements ExecutorService {

    @Autowired
    private LinuxShellComponent linuxShellComponent;

    @Value("${qemu.qmp.port}")
    private String qmpPort;

    @Value("${qemu.common.port}")
    private String commonPort;

    @Value("${qemu.serial.port}")
    private String serialPort;

    @Async
    @Override
    public void execLinuxCommod(String commod, String type) {
        linuxShellComponent.login();
        try {
            linuxShellComponent.executeShell(commod,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async
    @Override
    public void execLinuxCommodSync(String commod) {
        linuxShellComponent.login();
        try {
            linuxShellComponent.executeShell(commod,Consts.TYPE_COMMON);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void execLinuxCommodBash(String commod) {
        linuxShellComponent.login();
        try {
            linuxShellComponent.executeShell(commod,Consts.TYPE_COMMON);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void execLinuxCommodBashNoBack(String commod) {
        linuxShellComponent.login();
        try {
            linuxShellComponent.executeShellNoBack(commod);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startAllTest(String commond) throws InterruptedException {
        // 启动nc
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("nc -l -p " + commonPort,Consts.TYPE_COMMON);
            }
        }).start();
        System.out.println("执行NC完成");
        // 启动start脚本
        if (StrUtil.isBlank(commond)) {
            commond = "bash /home/ubuntu/qemu-stm32/build/arm-softmmu/test.sh";
        } else {
            commond = "bash " + commond;
        }
        Thread.sleep(10*1000L);
        String finalCommand = commond;
        System.out.println("开始执行start test脚本");
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("sh /home/mcufw/stm32f407igt6-adc/build.sh","build");
//                execLinuxCommodBash(finalCommand);
            }
        }).start();
        System.out.println("执行完成");
    }

    @Override
    public void startAll(String commond) throws InterruptedException {
        // 启动nc
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("nc -l -p " + commonPort, Consts.TYPE_COMMON);
            }
        }).start();
        System.out.println("执行NC完成");
        // 启动start脚本
        if (StrUtil.isBlank(commond)) {
            //--firmware /home/mcufw/stm32f407igt6-adc/bin/stm32f407igt6-adc.elf
            commond = "bash /opt/qemu-stm32/start.sh  --machine yunfeng-es1 --firmware /opt/workspace/stm32f103rbt6-fw_led/.pio/build/genericSTM32F103RB/firmware.elf  --qmp 24" + " --serial " + serialPort + " --output " +commonPort;
        } else {
            commond = "bash " + commond;
        }
        Thread.sleep(3*1000L);
        String finalCommand = commond;
        System.out.println("开始执行start脚本");
        new Thread(new Runnable() {
            @Override
            public void run() {
               try {
                   execLinuxCommodBash(finalCommand);
               } catch (Exception e) {
                   e.printStackTrace();
               }
            }
        }).start();
        Thread.sleep(2*1000L);
        System.out.println("开始执行ncat脚本");
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommodBashNoBack("ncat 0.0.0.0 24");
            }
        }).start();
        Thread.sleep(2*1000L);
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommodSync("kill -9 `ps -ef | grep ncat | grep -v grep | awk '{print $2}'`");
            }
        }).start();
        Thread.sleep(1*1000L);
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("ncat localhost " + serialPort,Consts.TYPE_SERIAL);
            }
        }).start();
        System.out.println("执行完成");
    }

    @Override
    public void debugAll(String commond) throws InterruptedException {
        // 启动nc
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("nc -l -p " + commonPort, Consts.TYPE_COMMON);
            }
        }).start();
        System.out.println("执行NC完成");
        // 启动start脚本
        if (StrUtil.isBlank(commond)) {
            //--firmware /home/mcufw/stm32f407igt6-adc/bin/stm32f407igt6-adc.elf
            commond = "bash /opt/qemu-stm32/debug.sh  --machine yunfeng-es1 --firmware /opt/workspace/stm32f103rbt6-fw_led/.pio/build/genericSTM32F103RB/firmware.elf  --qmp 24" + " --serial " + serialPort + " --output " +commonPort;
        } else {
            commond = "bash " + commond;
        }
        Thread.sleep(3*1000L);
        String finalCommand = commond;
        System.out.println("开始执行debug脚本");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    execLinuxCommodBash(finalCommand);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Thread.sleep(2*1000L);
        System.out.println("开始执行ncat脚本");
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommodBashNoBack("ncat 0.0.0.0 24");
            }
        }).start();
        Thread.sleep(2*1000L);
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommodSync("kill -9 `ps -ef | grep ncat | grep -v grep | awk '{print $2}'`");
            }
        }).start();
        Thread.sleep(1*1000L);
        new Thread(new Runnable() {
            @Override
            public void run() {
                execLinuxCommod("ncat localhost " + serialPort,Consts.TYPE_SERIAL);
            }
        }).start();
        System.out.println("执行完成");
    }

    @Override
    public void stopAll() {
        // stop nc
        execLinuxCommodSync("kill -9 `ps -ef | grep -w nc | grep -v grep | awk '{print $2}'`");

        execLinuxCommodSync("kill -9 `ps -ef | grep -w ncat | grep -v grep | awk '{print $2}'`");

        // stop 脚本
        execLinuxCommodBash("kill -9 `ps -ef | grep qemu-system-arm | grep -v grep | awk '{print $2}'`");
    }
}
